package com.sfh.cpc.com.refresh;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.DimenRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sfh.cpc.com.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:刷新加载更多控件
 *
 * @author SunFeihu 孙飞虎
 * @date 2017/7/12
 */
public class SwipeRefreshMoreLayout extends SwipeRefreshLayout implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {


    /**
     * 视图显示控件
     */
    protected RecyclerView mRecyclerView;
    /**
     * 适配器
     */
    protected BaseQuickAdapter pullToRefreshAdapter;
    /**
     * 加载更多与刷新回调
     */
    protected OnLoadingListener mListener;
    /**
     * 延迟刷新时间【毫秒】
     */
    protected long time = 250L;

    /**
     * 无数据对象
     */
    protected View emptyView;


    public SwipeRefreshMoreLayout(@NonNull Context context) {

        super(context);
        this.init();
    }

    public SwipeRefreshMoreLayout(@NonNull Context context, @Nullable AttributeSet attrs) {

        super(context, attrs);
        this.init();
    }


    public interface OnLoadingListener {

        /***刷新*/
        void onRefreshUI();

        /***加载更多*/
        void onLoadMoreUI();
    }


    private void init() {
        this.mRecyclerView = new RecyclerView(this.getContext());
        this.addView(this.mRecyclerView);
    }

    /***
     * 设置适配器
     * @param adapter
     * @param loadMore true 表示刷新+加载更多 false 只有刷新功能
     */
    public void setAdapter(BaseQuickAdapter adapter, boolean loadMore) {

        if (adapter == null) {
            return;
        }
        this.pullToRefreshAdapter = adapter;

        this.setColorSchemeColors(Color.parseColor("#3c75ed"));
        // 刷新
        this.setOnRefreshListener(this);
        //线性
        this.mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getContext()));
        // 加载动画
        this.pullToRefreshAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        // 先绑定RecyclerView
        this.pullToRefreshAdapter.bindToRecyclerView(mRecyclerView);
//        this.pullToRefreshAdapter.setEmptyView(R.layout.widget_refresh_empty_layout);
        if (loadMore) {
            // 加载更多
            this.pullToRefreshAdapter.setOnLoadMoreListener(this, mRecyclerView);
            // 默认不加载更多
            this.pullToRefreshAdapter.disableLoadMoreIfNotFullPage();
            //自定义加载布局
            this.pullToRefreshAdapter.setLoadMoreView(new CustomLoadMoreView());
        }
    }

    /***
     * 设置Iitem 项间距
     * @param dimen 资源ID
     */
    public void addItemDecoration(@DimenRes int dimen) {


        if (this.mRecyclerView == null) {
            return;
        }
        int top = getResources().getDimensionPixelOffset(dimen);
        this.mRecyclerView.addItemDecoration(new SpaceItemDecoration(top));
    }

    public void addOnScrollListener(RecyclerView.OnScrollListener listener){
        this.mRecyclerView.addOnScrollListener(listener);
    }
    /***
     * 设置Iitem 项间距
     * @param dimen 数值
     */
    public void addItemDecorationSize(int dimen) {
        if (this.mRecyclerView == null) {
            return;
        }
        this.mRecyclerView.addItemDecoration(new SpaceItemDecoration(dimen));
    }

    /***
     * 【先绑定适配器】
     * 设置无数据时空白页，默认R.layout.base_list_empty_view
     */
    public View setEmptyView(int layout) {

        this.emptyView = View.inflate(getContext(), layout, null);
        return this.emptyView;
    }

    /***
     * 【先绑定适配器】
     * 设置无数据时空白页，默认R.layout.base_list_empty_view
     */
    public void setEmptyView(View emptyView) {

        if (this.pullToRefreshAdapter == null) {
            return;
        }
        this.emptyView = emptyView;
    }


    /***
     * 设置内容边距
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setRecyclerViewPadding(int left, int top, int right, int bottom) {

        if (this.mRecyclerView == null) {
            return;
        }
        this.mRecyclerView.setPadding(left, top, right, bottom);
    }

    /***
     * 设置RecyclerView 布局模式【默认线性】
     */
    public void setLayoutManager(RecyclerView.LayoutManager layout) {
        if (this.mRecyclerView == null) {
            return;
        }
        //线性
        this.mRecyclerView.setLayoutManager(layout);
    }

    @Override
    public void onRefresh() {
        if (this.pullToRefreshAdapter == null) {
            return;
        }
        this.pullToRefreshAdapter.setEnableLoadMore(false);
        if (this.mListener != null) {
            this.mListener.onRefreshUI();
        }
    }

    @Override
    public void onLoadMoreRequested() {

        if (this.mListener != null) {
            this.mListener.onLoadMoreUI();
        }
    }

    /****
     * 刷新+加载更多回调
     * @param mListener
     */
    public void setOnLoadingListener(OnLoadingListener mListener) {

        this.mListener = mListener;
    }

    /***
     * item view 中控件点击事件
     * @param listener
     */
    public void setOnItemChildClickListener(BaseQuickAdapter.OnItemChildClickListener listener) {
        if (this.pullToRefreshAdapter == null) {
            return;
        }
        //子控件点击事件
        this.pullToRefreshAdapter.setOnItemChildClickListener(listener);
    }

    /*** item 点击事件 */
    public void setOnItemClickListener(@Nullable BaseQuickAdapter.OnItemClickListener listener) {
        if (this.pullToRefreshAdapter == null) {
            return;
        }
        //Item 点击事件
        this.pullToRefreshAdapter.setOnItemClickListener(listener);
    }

    /***
     * 是否全部加载完成 【外部调用】
     */
    public void onLoadAllCompale() {
        if (this.pullToRefreshAdapter == null) {
            return;
        }
        this.pullToRefreshAdapter.loadMoreEnd();
        this.closeRefresh();
        this.showEmptyView();
    }


    /***
     * 获取更多数据失败 【外部调用】
     *
     */
    public void onLoadMoreFail() {
        if (this.pullToRefreshAdapter == null) {
            return;
        }
        this.pullToRefreshAdapter.loadMoreFail();
        this.closeRefresh();
        this.showEmptyView();
    }

    /***
     * 完成数据加载【外部调用】
     *
     * @param data 数据
     * @param loadMore true 加载更多完成 false 刷新完成
     */
    public void onRefreshCompale(final List data, boolean loadMore) {
        if (this.pullToRefreshAdapter == null) {
            return;
        }

        List temp = data == null ? new ArrayList(0) : data;

        if (loadMore) {
            //成功获取更多数据
            this.pullToRefreshAdapter.addData(temp);
            this.pullToRefreshAdapter.loadMoreComplete();
        } else {

            this.pullToRefreshAdapter.setNewData(temp);
            this.pullToRefreshAdapter.setEnableLoadMore(true);
            this.closeRefresh();
            this.showEmptyView();
        }
    }


    /***
     * 数据为空
     */
    private void showEmptyView() {
        if (this.pullToRefreshAdapter == null) {
            return;
        }
        List data = this.pullToRefreshAdapter.getData();
        if (data != null && !data.isEmpty()) {
            return;
        }
        View empty = this.pullToRefreshAdapter.getEmptyView();
        if (empty != null || this.emptyView == null) {
            return;
        }
        this.pullToRefreshAdapter.setEmptyView(this.emptyView);
    }

    /***
     * 禁止下拉刷新
     * @param enabled
     */
    public void setForbidRefreshLayout(boolean enabled) {
        this.setEnabled(enabled);
    }


    /**
     * 关闭刷新
     */
    private void closeRefresh() {

        this.postDelayed(closeRefresh, time);
    }

    private Runnable closeRefresh = new Runnable() {

        @Override
        public void run() {
            SwipeRefreshMoreLayout.this.removeCallbacks(this);
            SwipeRefreshMoreLayout.this.setRefreshing(false);
        }
    };

    /****
     * 自动刷新【外部调用】
     */
    public void onAutoRefresh() {

        // 已正在刷新
        if (this.isRefreshing()) {
            return;
        }

        this.postDelayed(autoRefresh, time);
    }

    private Runnable autoRefresh = new Runnable() {

        @Override
        public void run() {
            SwipeRefreshMoreLayout.this.removeCallbacks(this);
            SwipeRefreshMoreLayout.this.setRefreshing(true);
            if (mListener != null) {
                mListener.onRefreshUI();
            }
        }
    };

    @Override
    protected void onDetachedFromWindow() {
        this.removeCallbacks(closeRefresh);
        this.removeCallbacks(autoRefresh);
        super.onDetachedFromWindow();
    }


}
